package org.jflame.devAide.plugin.codeFormatter;

import java.util.List;

import org.jflame.commons.exception.ConvertException;
import org.jflame.commons.util.CollectionHelper;
import org.jflame.devAide.util.ResourceUtils;

class ComplexFormatter implements CodeFormatter {

    private List<CodeFormatter> formatters;
    private CodeFormatter currentFormatter;

    public ComplexFormatter(List<CodeFormatter> formatters) {
        this.formatters = formatters;
    }

    @Override
    public String convert(String source) {
        if (CollectionHelper.isNotEmpty(formatters)) {
            for (CodeFormatter f : formatters) {
                if (f.isSupported(source)) {
                    try {
                        return currentFormatter.convert(source);
                    } catch (ConvertException e) {

                    }
                }
            }
        }

        throw new ConvertException("不支持的格式");
    }

    @Override
    public boolean isSupported(String text) {
        return formatters.stream()
                .anyMatch(f -> f.isSupported(text));
    }

    @Override
    public String getCodeFormat() {
        return ResourceUtils.getString("plugin.formatter.auto");
    }

    public void setFormatters(List<CodeFormatter> formatters) {
        this.formatters = formatters;
    }

    public CodeFormatter getCurrentFormatter() {
        return currentFormatter;
    }

}
